package cn.juque.systemservice.service.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.juque.common.annotation.HandlerAppException;
import cn.juque.common.base.BaseOperator;
import cn.juque.common.base.BaseResponseDTO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.utils.ValidatorUtil;
import cn.juque.systemservice.api.dto.sms.AddSmsLogDTO;
import cn.juque.systemservice.api.dto.sms.CheckCodeDTO;
import cn.juque.systemservice.api.dto.sms.SmsLogDTO;
import cn.juque.systemservice.api.dto.sms.SmsLogSearchDTO;
import cn.juque.systemservice.api.provider.ISmsLogProvider;
import cn.juque.systemservice.entity.SmsLog;
import cn.juque.systemservice.service.ISmsLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <li>simple-generator</li>
 * <li></li>
 * @date 2021-09-30 14:39:45
 **/
@Slf4j
@Service("smsLogProvider")
@HandlerAppException
public class SmsLogProviderImpl implements ISmsLogProvider {

    @Resource
    private ISmsLogService smsLogService;

    @Resource
    private ValidatorUtil validatorUtil;

    /**
     * 列表检索
     *
     * @param req 入参
     * @return List
     */
    @Override
    public BaseResponseDTO<List<SmsLogDTO>> listLog(SmsLogSearchDTO req) {
        PageInfo pageInfo = req.getPageInfo();
        SmsLog smsLog = new SmsLog();
        smsLog.setTel(req.getTel());
        List<SmsLog> logList = this.smsLogService.listLogWithPage(pageInfo, smsLog);
        List<SmsLogDTO> logDtoList = BeanUtil.copyToList(logList, SmsLogDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, logDtoList, pageInfo);
    }

    /**
     * 新增
     *
     * @param smsLogDto AddSmsLogDTO
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> addLog(AddSmsLogDTO smsLogDto) {
        this.validatorUtil.fastValidate(smsLogDto);
        BaseOperator baseOperator = smsLogDto.getBaseOperator();
        this.smsLogService.saveLog(smsLogDto.getTel(), smsLogDto.getContent(), smsLogDto.getCheckCodeNum(), smsLogDto.getValidSecond(), baseOperator);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 校验
     *
     * @param checkCodeDTO CheckCodeDTO
     * @return boolean
     */
    @Override
    public BaseResponseDTO<Boolean> checkCode(CheckCodeDTO checkCodeDTO) {
        this.validatorUtil.fastValidate(checkCodeDTO);
        boolean flag = this.smsLogService.checkCode(checkCodeDTO.getTel(), checkCodeDTO.getCode());
        return new BaseResponseDTO<>(MessageEnum.OK, flag);
    }
}
